<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <page-form
      class="paddingBox order-model-formBox flex_wrap_start_start"
      v-model:ref-obj="formInfo.ref"
      :data="formInfo.data"
      :field-list="formInfo.fieldList"
      :label-width="formInfo.labelWidth"
      :rules="formInfo.rules"
    >
      <template v-slot:form-actualStartTime>
        <el-date-picker
          v-model="formInfo.data.actualStartTime"
          type="datetime"
          format="YYYY-MM-DD HH:mm"
          value-format="YYYY-MM-DD HH:mm"
          placeholder="实际上机时间"
        >
        </el-date-picker>
      </template>
    </page-form>
  </page-dialog>
</template>

<script setup name="equipmentUpDownEnable">
  import {
    enabledUseManage
  } from '@/api/asset.js'
  import { ElMessage } from 'element-plus'
const emits = defineEmits();
const { proxy } = getCurrentInstance();
import utils from '@/common/js/utils'
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Object,
    default: function () {
      return {}
    },
  },
})

const formInfo = ref({
  ref: null,
  data: {},
  fieldList: [
    {
      label: '资产名称',
      value: 'assetName',
      type: 'input',
      disabled: true,
    },
    {
      label: '资产卡片号',
      value: 'assetCode',
      type: 'input',
      disabled: true,
    },
    { label: '病人编号', value: 'patientCode', type: 'input', disabled: true },
    { label: '病人姓名', value: 'patientName', type: 'input', disabled: true },
    {
      label: '性别',
      value: 'sex',
      type: 'select-model',
      code: 'sex',
      disabled: true,
    },
    { label: '年龄', value: 'age', type: 'number', disabled: true },
    {
      label: '预计上机时间',
      value: 'estimateStartTime',
      type: 'input',
      disabled: true,
    },
    { label: '实际上机时间', value: 'actualStartTime', type: 'slot' },
    {
      label: '检查/治疗时间(分钟)',
      value: 'estimateTreatDuration',
      type: 'number',
    },
    {
      label: '预计下机时间',
      value: 'estimateEndTime',
      type: 'input',
      disabled: true,
    },
    {
      label: '下机提前提醒(分钟)',
      value: 'remindBefore',
      type: 'number',
      min: -1
    },
    {
      label: '提醒时间',
      value: 'remindTime',
      type: 'input',
      disabled: true,
    },
  ],
  rules: {
    estimateTreatDuration: [
      { required: true, message: '请输入检查/治疗时间', trigger: 'blur' },
    ],
    remindBefore: [
      { required: true, message: '请输入下机提前提醒', trigger: 'blur' },
    ],
    actualStartTime: [
      { required: true, message: '请选择实际上机时间', trigger: 'change' },
    ],
  },
  labelWidth: '140px',
})

// 弹窗相关
const dialogInfo = ref({
  title: '启动',
  width: '80%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      event: save,
      show: true,
    },
  ],
})

const shows = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件shows
  },
})

watch(
  () => formInfo.value.data.actualStartTime,
  () => {
    setEstimateEndTime()
  }
)
watch(
  () => formInfo.value.data.estimateTreatDuration,
  () => {
    setEstimateEndTime()
  }
)

function setEstimateEndTime() {
  let fmartActualStartTime = new Date(
    formInfo.value.data.actualStartTime
  ).getTime()
  formInfo.value.data.estimateEndTime = utils.switchTime(
    fmartActualStartTime +
      formInfo.value.data.estimateTreatDuration * 60 * 1000,
    'YYYY-MM-DD hh:mm'
  )
}
watch(
  () => formInfo.value.data.estimateEndTime,
  () => {
    setRemindTime()
  }
)
watch(
  () => formInfo.value.data.remindBefore,
  () => {
    setRemindTime()
  }
)

function setRemindTime() {
  let fmartEstimateEndTime = new Date(
    formInfo.value.data.estimateEndTime
  ).getTime()
  formInfo.value.data.remindTime = utils.switchTime(
    fmartEstimateEndTime - formInfo.value.data.remindBefore * 60 * 1000,
    'YYYY-MM-DD hh:mm'
  )
}

onMounted(() => {
  formInfo.value.data  = props.data
  formInfo.value.data.actualStartTime  = formInfo.value.data.estimateStartTime
})


function save() {
  if(formInfo.value.data.estimateTreatDuration <= formInfo.value.data.remindBefore) {
        ElMessage({
          type: 'warning',
          message: '检查/治疗时间应大于下机提醒时间',
        })
        return
      }
  enabledUseManage(formInfo.value.data).then(res => {
    if(res.success) {
      ElMessage({
        type: 'success',
        message: '操作成功',
      })
      handleClose()
    }
  })
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('callback')
}
</script>

<style scoped>
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
